package com.bawei.module_motocircle.ui.post.detail

import android.util.Log
import com.bawei.lib_common.base.BaseViewModel
import com.bawei.lib_common.base.state.Loading
import com.bawei.lib_common.base.state.RequestEvent
import com.bawei.lib_common.base.state.Success
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.zip
import javax.inject.Inject

/**
 * @author yaotianxue
 * @description
 * @date 2025/6/3
 */
@HiltViewModel
class PostDetailViewModel @Inject constructor(private val repository: PostDetailRepository): BaseViewModel() {

    var currentPost:PostItem? = null
    var afterSale:List<AfterSalePageItem>? = null
    var comments:List<CommentItem>? = null

    private val mPostState_:MutableStateFlow<RequestEvent> = MutableStateFlow(Loading)
    val mPostState = mPostState_.asStateFlow()


    fun getPostDetailData(){
        execute(
            action = repository.getPostDetail(),
            onError = {

            },
            onFinish = {
                currentPost = it.data
                if(it != null){
                    repository.getAfterSale() //售后信息
                        .zip(repository.getComments()) { afterSale, comments -> //评论信息
                            this.afterSale = afterSale.data
                            this.comments = comments.data
                            currentPost //帖子信息
                        }.collect{ //帖子信息
                            if (it != null){
                                mPostState_.value = Success
                            }

                        }
                }


            }

        )
    }


}